package com.example.administrator.shzs.adapter;

import android.content.Context;
import android.content.SearchRecentSuggestionsProvider;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.administrator.shzs.R;
import com.example.administrator.shzs.entity.UserAddressData.AddressData;

import java.util.List;

/**
 * Created by Administrator on 2016/11/4.
 */

public class ShippingAddressAdapter extends RecyclerView.Adapter<ShippingAddressAdapter.MyViewHolder> {

    private Context context;
    private List<AddressData> addressDatas;
    private MyViewHolder holder;

    public ShippingAddressAdapter(Context context, List<AddressData> addressDatas) {
        this.context = context;
        this.addressDatas = addressDatas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view=View.inflate(context,R.layout.shipping_address_item,null);
        holder=new MyViewHolder(view);
        return holder;

    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        String dataId=addressDatas.get(position).getDataid();
        String name=addressDatas.get(position).getReceiver();
        String address=addressDatas.get(position).getAddress();
        String phone=addressDatas.get(position).getMobilephone();

        holder.tvName.setText("姓名："+name);
        holder.tvAddress.setText("收货地址:"+address);
        holder.tvPhone.setText("手机号:"+phone);

        if (addressDatas.size() > 0){
            if (position == 0){
                holder.checkBox.setVisibility(View.VISIBLE);
            }else {
                holder.checkBox.setVisibility(View.GONE);
            }

        }else {

        }

        setLinsteners(holder,name,address,phone,dataId);

    }

    private void setLinsteners(final MyViewHolder holder, final String name, final String address, final String phone, final String dataId) {

        if(mOnItemClickListener!=null) {
            holder.tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {//删除
                    mOnItemClickListener.onDeleteClick(holder,name,address,phone,dataId);
                }
            });
        }


        if(mOnItemClickListener!=null) {//编辑
            holder.tvCompile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onCompileClick(holder,name,address,phone,dataId);
                }
            });
        }

        if(mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(v,name,address,phone);
                }
            });
        }
    }


    public void removeData(int position) {
        addressDatas.remove(position);
        notifyItemRemoved(position);
    }


    @Override
    public int getItemCount() {
        return addressDatas == null ? 0 : addressDatas.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        private TextView tvName;
        private TextView tvAddress;
        private TextView tvPhone;
        public TextView tvDelete;
        public TextView tvCompile;
        public CheckBox checkBox;

        public MyViewHolder(View itemView) {
            super(itemView);
            checkBox= (CheckBox) itemView.findViewById(R.id.tv_checkbox);
            tvName= (TextView) itemView.findViewById(R.id.tv_name_shipping_address_item);
            tvPhone= (TextView) itemView.findViewById(R.id.tv_phone_shipping_address_item);
            tvAddress= (TextView) itemView.findViewById(R.id.tv_address_shipping_address_item);
            tvDelete= (TextView) itemView.findViewById(R.id.tv_delete);
            tvCompile= (TextView) itemView.findViewById(R.id.tv_compile);
        }
    }



    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view,String name,String address,String phone);
        void onDeleteClick(MyViewHolder holder,String name,String address,String phone,String dataId);
        void onCompileClick(MyViewHolder holder,String name,String address,String phone,String dataId);
    }
}
